package battlestein;
import gamer.IA;
import gamer.Player;

import java.io.IOException;

import dispatcher.LocalDispatcher;
import dispatcher.ServerDispatcher;
	public class GameEngine{

		private ServerDispatcher dpServer;
		private LocalDispatcher dpLocal;
		private BattleWar bw;
		
		private String network;
		private boolean start;
		private Player p;
		private IA[] ia;

		private int players;
		
		public GameEngine() throws IOException {

			bw = new BattleWar();
			this.dpServer = new ServerDispatcher(this, 3002);
			this.dpLocal = new LocalDispatcher(this);
			this.p = new Player(2,"player",dpLocal, 0);
			this.dpLocal.setGamer(p);
			
			players = 0;
			start = false;
		}
		
		public void update() {
			try {
				p.play();
				if(start){
					for(IA bot : ia) {
						bot.play();
					}
				}
				synchronized(bw) {
					bw.update();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		public void setNetwork(String network) {
			this.network = network;
		}
		
		public void newPlayer() {
			if(players == 0) {
				if(network.equals("Local")) {
					this.startGame();
				}
				players++;
			}
			else if(players == 1) {
				if(network.equals("Reseau")) {
					this.startGame();
				}
				players++;
			}
		}
		
		public void startGame() {
			if(network.equals("Reseau")) {
				this.ia = new IA[2];
				this.ia[0] = new IA(3,"DumbIA",dpLocal);
				this.ia[1] = new IA(4,"DumbIA",dpLocal);
				this.dpLocal.setGamer(ia[0]);
				this.dpLocal.setGamer(ia[1]);
			}
			else if(network.equals("Local")) {
				this.ia = new IA[3];
				this.ia[0] = new IA(3,"DumbIA",dpLocal);
				this.ia[1] = new IA(4,"DumbIA",dpLocal);
				this.ia[2] = new IA(1,"DumbIA",dpLocal);
				this.dpLocal.setGamer(ia[0]);
				this.dpLocal.setGamer(ia[1]);
				this.dpLocal.setGamer(ia[2]);
			}
			start = true;
			dpServer.startGame(1);
			dpLocal.startGame(2);
		}

		public void updateTower(int id, int idPlayer, int x, int y) {
			dpLocal.updateTower(id, idPlayer, x, y);
			dpServer.updateTower(id, idPlayer, x, y);
		}
		
		public void updateTower(int id, int rate, int range, int life, int velocity) {
			dpLocal.updateTower(id, rate, range, life, velocity);
			dpServer.upgradeTower(id, rate, range, life, velocity);
		}

		public void setDestBase(int src, int dest) {
			synchronized(bw) {
				bw.setDestBase(src, dest);
			}
			dpLocal.updateDestBase(src, dest);
			dpServer.updateDestBase(src, dest);
		}

		public void upgradeTower(int id, int rate, int range, int life, int velocity) {
			synchronized(bw) {
				bw.upgradeTower(id, rate, range, life, velocity);
			}
			dpLocal.updateTower(id, rate, range, life, velocity);
			dpServer.updateTower(id, rate, range, life, velocity);
		}

		public void addTower(int id, int idPlayer, int x, int y) {
			synchronized(bw) {
				if(bw.addTower(id, idPlayer, x, y)) {
					this.updateTower(id, idPlayer, x, y);
				}
			}
		}

		
		public ServerDispatcher getDpServer() {
			return dpServer;
		}

		public void setDpServer(ServerDispatcher dpServer) {
			this.dpServer = dpServer;
		}

		public LocalDispatcher getDpLocal() {
			return dpLocal;
		}

		public void setDpLocal(LocalDispatcher dpLocal) {
			this.dpLocal = dpLocal;
		}

		public BattleWar getBw() {
			return bw;
		}

		public void setBw(BattleWar bw) {
			this.bw = bw;
		}
		
		public static void main(String[] args) throws IOException {
			GameEngine GE = new GameEngine();
			while(true) {
				GE.update();
			}
		}

	}
